<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="厂商" prop="mfg">
        <el-select v-model="queryParams.mfg" placeholder="请选择厂商" clearable>
          <el-option
            v-for="dict in dict.type.model_mfg"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="模型类型" prop="modelType">
        <el-select v-model="queryParams.modelType" placeholder="请选择模型类型" clearable>
          <el-option
            v-for="dict in dict.type.model_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否启用" prop="enable">
        <el-select v-model="queryParams.enable" placeholder="请选择是否启用" v-hasPermi="['ai:modelConfig:edit']"  clearable>
          <el-option
            v-for="dict in dict.type.common_yesno"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ai:modelConfig:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ai:modelConfig:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ai:modelConfig:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ai:modelConfig:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="modelConfigList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" v-if="true"/>
      <el-table-column label="厂商" align="center" prop="mfg">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.model_mfg" :value="scope.row.mfg"/>
        </template>
      </el-table-column>
      <el-table-column label="模型类型" align="center" prop="modelType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.model_type" :value="scope.row.modelType"/>
        </template>
      </el-table-column>
      <el-table-column label="appid" align="center" prop="appid" />
      <el-table-column label="apiKey" align="center" prop="apiKey" />
      <el-table-column label="secretKey" align="center" prop="secretKey" />
      <el-table-column label="单次token数限制" align="center" prop="maxTokens" />
      <el-table-column label="是否启用" align="center" prop="enable">
<!--        <template slot-scope="scope">
          <dict-tag :options="dict.type.common_yesno" :value="scope.row.enable"/>
        </template>-->
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.enable"
            :active-value="1"
            :inactive-value="0"
            @change="handleEnableChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="是否深度思考" align="center" prop="isDeepseek">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.common_yesno" :value="scope.row.isDeepseek"/>
        </template>
      </el-table-column>
      <el-table-column label="是否输出思考过程" align="center" prop="isOutThink">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.common_yesno" :value="scope.row.isOutThink"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ai:modelConfig:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ai:modelConfig:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改模型配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="厂商" prop="mfg">
          <el-select v-model="form.mfg" placeholder="请选择厂商">
            <el-option
              v-for="dict in dict.type.model_mfg"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="模型类型" prop="modelType">
          <el-select v-model="form.modelType" placeholder="请选择模型类型">
            <el-option
              v-for="dict in dict.type.model_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="appid" prop="appid">
          <el-input v-model="form.appid" placeholder="请输入appid" />
        </el-form-item>
        <el-form-item label="apiKey" prop="apiKey">
          <el-input v-model="form.apiKey" placeholder="请输入apiKey" />
        </el-form-item>
        <el-form-item label="secretKey" prop="secretKey">
          <el-input v-model="form.secretKey" placeholder="请输入secretKey" />
        </el-form-item>
        <el-form-item label="单次token数限制" prop="maxTokens" label-width="120px">
          <el-input v-model="form.maxTokens" placeholder="目前支持阿里、百度的大模型配置" />
        </el-form-item>
        <el-form-item label="深度思考" prop="isDeepseek" label-width="120px">
          <el-select v-model="form.isDeepseek" placeholder="请选择是否深度思考">
            <el-option
              v-for="dict in dict.type.common_yesno"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="输出思考过程" prop="isOutThink" label-width="120px">
          <el-select v-model="form.isOutThink" placeholder="请选择是否输出思考过程">
            <el-option
              v-for="dict in dict.type.common_yesno"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listModelConfig, getModelConfig, delModelConfig, addModelConfig, updateModelConfig } from "@/api/ai/modelConfig";
import {changeOssConfigStatus} from "@/api/system/ossConfig";

export default {
  name: "ModelConfig",
  dicts: ['common_yesno', 'model_type', 'model_mfg'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 模型配置表格数据
      modelConfigList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mfg: undefined,
        modelType: undefined,
        enable: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "id不能为空", trigger: "blur" }
        ],
        mfg: [
          { required: true, message: "厂商不能为空", trigger: "change" }
        ],
        modelType: [
          { required: true, message: "模型类型不能为空", trigger: "change" }
        ],
        apiKey: [
          { required: true, message: "apiKey不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询模型配置列表 */
    getList() {
      this.loading = true;
      listModelConfig(this.queryParams).then(response => {
        this.modelConfigList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        mfg: undefined,
        modelType: undefined,
        apiKey: undefined,
        secretKey: undefined,
        maxRounds: undefined,
        maxTokens: undefined,
        enable: undefined,
        createTime: undefined,
        createBy: undefined,
        updateTime: undefined,
        updateBy: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加模型配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getModelConfig(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改模型配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateModelConfig(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addModelConfig(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除模型配置编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delModelConfig(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ai/modelConfig/export', {
        ...this.queryParams
      }, `modelConfig_${new Date().getTime()}.xlsx`)
    },
    // 对象存储配置状态修改
    handleEnableChange(row) {
      let text = row.enable === "1" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.mfg + '"配置吗?').then(() => {
        const data = {
          "id": row.id,
          "enable": row.enable
        }
        return updateModelConfig(data);
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess(text + "成功");
      }).catch(() => {
        row.status = row.status === "0" ? "1" : "0";
      })
    }
  }
};
</script>
